﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;
using Mimoza.Common;

namespace Mimoza.DBLayer.SQLSC
{
    class SQLCS_DBPolicy: Mimoza.Common.IDBPolicyRepository
    {
       private SqlCeConnection connection = null;

        private SQLCS_DBPolicy()
        { }

        public SQLCS_DBPolicy(SqlCeConnection conn)
        {
            connection = conn;
        }

        #region IDBPolicy Members

        public Mimoza.Common.PolicyRecord GetPolicy(Guid ID)
        {
            SqlCeCommand cmd = null;
            ContainerRecord record = new ContainerRecord();

            try
            {
                cmd = connection.CreateCommand();
                cmd.CommandText = SQLCS_Helper.SelectPolicyString + " WHERE [ID] = @ID";
                cmd.Parameters.AddWithValue("@ID", ID);

                SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Scrollable);
                string[] columns = rs.ResultSetView.Columns;

                if (!rs.ReadFirst()) return null;

                return SQLCS_Helper.PolicyRecordFromResultSet(rs, columns);
            }
            finally
            {
                if (null != cmd) cmd.Dispose();
            }           
        }

        public IEnumerable<Mimoza.Common.PolicyRecord> GetPolicies(Guid containerID)
        {
            SqlCeCommand cmd = null;

            try
            {
                cmd = connection.CreateCommand();
                cmd.CommandText = SQLCS_Helper.SelectPolicyString + " WHERE [ParentID] = @ParentID";
                cmd.Parameters.AddWithValue("@ParentID", containerID);

                SqlCeResultSet rs = cmd.ExecuteResultSet(ResultSetOptions.Scrollable);
                string[] columns = rs.ResultSetView.Columns;

                if (!rs.ReadFirst()) yield break;

                do
                {
                    yield return SQLCS_Helper.PolicyRecordFromResultSet(rs, columns);
                } while (rs.Read());
            }
            finally
            {
                if (null != cmd) cmd.Dispose();
            }           
        }

        public void Insert(Mimoza.Common.PolicyRecord policy)
        {
            SqlCeCommand cmd = null;

            try
            {
                if (policy.ID == null || policy.ID == Guid.Empty)
                {
                    policy.ID = Guid.NewGuid();
                }

                policy.UpdateDate = DateTime.Now;

                cmd = connection.CreateCommand();

                SQLCS_Helper.GreateInsertCommand(cmd, "Policy", policy);

                cmd.ExecuteNonQuery();

                RaiseOnObjectChanged(new DBObjectChangedEventArgs(policy, DBEventType.ObjectInserted));
            }
            finally
            {
                if (null != cmd) cmd.Dispose();
            }           
        }

        public void Update(Mimoza.Common.PolicyRecord policy)
        {
            SqlCeCommand cmd = null;

            try
            {
                policy.UpdateDate = DateTime.Now;

                cmd = connection.CreateCommand();

                SQLCS_Helper.GreateUpdateCommand(cmd, "Policy", policy);

                cmd.ExecuteNonQuery();

                RaiseOnObjectChanged(new DBObjectChangedEventArgs(policy, DBEventType.ObjectUpdated));
            }
            finally
            {
                if (null != cmd) cmd.Dispose();
            }           
        }

        public void Delete(Mimoza.Common.PolicyRecord policy)
        {
            SqlCeCommand cmd = null;

            try
            {
                cmd = connection.CreateCommand();
                cmd.CommandText = "DELETE FROM Policy WHERE [ID] = @ID";
                cmd.Parameters.AddWithValue("@ID", policy.ID);

                cmd.ExecuteNonQuery();

                RaiseOnObjectChanged(new DBObjectChangedEventArgs(policy, DBEventType.ObjectDeleted));
            }
            finally
            {
                if (null != cmd) cmd.Dispose();
            }           
        }

        public event EventHandler<DBObjectChangedEventArgs> OnObjectChanged;

        private void RaiseOnObjectChanged(DBObjectChangedEventArgs e)
        {
            EventHandler<DBObjectChangedEventArgs> handler = OnObjectChanged;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        #endregion
    }
}
